var token = localStorage.getItem("token"),
    latelyRelease = Sendajax("http://119.23.46.241:8080/home/search/recentlyreleased", "post", {start: 1}, "")
localStorage.removeItem("inDetailsPage")
setTimeout(function () {
    //显示最近发布
    showRelease(latelyRelease.data, "近期发布")
}, 100)

//显示动态
function showRelease(data, text) {
    if (data.length <= 3) {
        $("footer").css({"position": "absolute", "bottom": "0"})
    } else {
        $("footer").css({"position": ""})
    }

    let showData = `<section class="recently-released-text"><p>${text}</p></section>`
    if (data.length == 0) {
        showData += `<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`
    } else {
        $.each(data, function (i, val) {
            if (val['titImg']) {
                showData += `<section class="user-images-recently-released">
            <a href="detailsPage.html" data-detailsId="${val['id']}">
                <section class="user-images-recently-released-content">
                    <section class="user-images-recently-released-title">${val['title']}</section>
                    <section class="user-images-recently-released-text">${val['content']}</section>
                    <section class="user-images-recently-released-address-label">
                       <section class="user-images-recently-released-address">${val['position']}</section>
                       <section class="user-images-recently-released-label">${val['label']}</section>
                    </section>
                    <section class="user-images-recently-released-personal-information">
                        <section class="user-images-recently-released-username">${val['username']}</section>
                        <section class="user-images-recently-released-time">${val['time']}</section>
                    </section>
                </section>
                <section class="user-images-recently-released-image">
                    <img src="${val['titImg']}" alt="">
                </section>
            </a>
        </section>`
            } else {
                if (val.header) {
                    showData += `<section class="user-text-recently-released">
            <a href="detailsPage.html" data-detailsId="${val['id']}">
                <section class="user-text-recently-released-information">
                    <section class="user-text-recently-released-head"><img src="${val['header']}" alt=""></section>
                    <section class="user-text-recently-released-personal-information">
                        <section class="user-text-recently-released-username">${val['username']}</section>
                        <section class="user-text-recently-released-time">${val['time']}</section>
                    </section>
                </section>
                <section class="user-text-recently-released-content">
                    <section class="user-text-recently-released-title">${val['title']}</section>
                    <section class="user-text-recently-released-text">${val['content']}</section>
                    <div class="user-text-recently-released-label">${val['label']}</div>
                </section>
                 <section class="user-text-recently-released-address"><p>${val['position']}</p><section class="user-text-recently-released-address-icon"></section></section>
            </a>
        </section>`
                } else {
                    showData += `<section class="user-text-recently-released">
            <a href="detailsPage.html" data-detailsId="${val['id']}">
                <section class="user-text-recently-released-information">
                    <section class="user-text-recently-released-head"><p>${val.name.substr(-2)}</p></section>
                    <section class="user-text-recently-released-personal-information">
                        <section class="user-text-recently-released-username">${val['username']}</section>
                        <section class="user-text-recently-released-time">${val['time']}</section>
                    </section>
                </section>
                <section class="user-text-recently-released-content">
                    <section class="user-text-recently-released-title">${val['title']}</section>
                    <section class="user-text-recently-released-text">${val['content']}</section>
                    <div class="user-text-recently-released-label">${val['label']}</div>
                </section>
                 <section class="user-text-recently-released-address"><p>${val['position']}</p><section class="user-text-recently-released-address-icon"></section></section>
            </a>
        </section>`
                }
            }
        })
        showData += `<section class="end">
        <section></section>
        <p>到底了</p>
        </section>`
    }
    $(".recently-released").html(showData)
    $("#container a").on("click", function () {
        localStorage.setItem("detailsId", $(this).attr('data-detailsId'))
    })
}

//搜索功能
$(".search-box input").keydown(function (e) {
    if (e.keyCode == 13) {
        let searchResult = Sendajax("http://119.23.46.241:8080/home/search/searchtitle", "post", {
            input: $(this).val(),
            start: 1
        }, "")
        if (searchResult.code == 201) {
            showRelease(searchResult.data, "搜索结果")
            $(this).blur()
        }
    }
}).on("focus", function () {
    $("footer").css("z-index", '-1')
})

//点击动态
$("#container a").on("click", function () {
    localStorage.setItem("detailsId", $(this).attr('data-detailsId'))
})